package com.fqgj.sentry.biz.store;

import com.fqgj.common.utils.JSONUtils;
import com.fqgj.common.utils.MD5;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.fqgj.sentry.api.domain.RiskApiResult;
import com.fqgj.sentry.api.domain.request.DataCallback;
import com.fqgj.sentry.api.domain.request.DataCallbackVo;
import com.fqgj.sentry.api.domain.request.DecisionVo;
import com.fqgj.sentry.biz.decision.callback.DecisionDataCallbackThread;
import com.fqgj.sentry.biz.store.es.data.Event;
import com.fqgj.sentry.biz.store.system.SystemColumnUtil;
import com.fqgj.sentry.biz.thread.BackstageThread;
import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.exception.ErrorCodeEnums;
import com.fqgj.sentry.manage.entity.McApplicationEntity;
import com.fqgj.sentry.manage.entity.McMerchantEntity;
import com.fqgj.sentry.manage.service.ApplicationService;
import com.fqgj.sentry.policy.client.RiskAntiRelationService;
import com.fqgj.sentry.policy.engine.domain.PolicySet;
import com.fqgj.sentry.policy.entity.RmRiskAntiRelationEntity;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by liupengpeng on 2017/10/17.
 */
@Component
public class DecisionDataSave implements Serializable {
    public static final Log LOGGER = LogFactory.getLog(DecisionDataSave.class);

    public static final Log orderJsonLogger = LogFactory.getLog("engine-order-json");
    public static final Log engineOnlineLogger = LogFactory.getLog("engine_es_online_json");
    public static final Log engineVerifyLogger = LogFactory.getLog("engine_es_verify_json");

    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private RiskAntiRelationService riskAntiRelationService;
    @Autowired
    private BackstageThread backstageThread;

    public void saveData(DecisionVo decisionVo, RiskApiResult riskResult, PolicySet policySet) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        Gson gson = new Gson();
        Long appId = policySet.getAppId();
        Long merchantId = policySet.getMerchantId();
        Date createdDate = new Date();
        LOGGER.info("== saveData ==,appId:{},merchantId:{}", appId, merchantId);
        return;
    }


    public void saveCallBackData(DataCallbackVo dataCallbackVo) {

        List<DataCallback> dataCallbackList = dataCallbackVo.getDataCallbackList();

        McMerchantEntity merchantEntity = applicationService.selectMerchantByMerchantCode(dataCallbackVo.getMerchantCode());
        McApplicationEntity applicationEntity = applicationService.selectByMerchantIdAndAppCode(merchantEntity.getId(),
                dataCallbackVo.getAppCode());

        Long appId = applicationEntity.getId();
        Long merchantId = merchantEntity.getId();
        backstageThread.threadPool.execute(new DecisionDataCallbackThread(dataCallbackList, appId, merchantId));
    }

}
